//
//  HomeHelperCell.swift
//  d11
//
//  Created by peale on 2018/8/7.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias HelperBtnClickBlock2 = (_ tag: Int) -> Void
class HomeHelperCell: UITableViewCell {
    var helperBtnClickBlock2: HelperBtnClickBlock2?
    func callClickBlock(_ block: @escaping HelperBtnClickBlock2) {
        
        helperBtnClickBlock2 = block
    }
    var iconImageView : UIImageView?
    
    var gridBtn1 : UIButton?
    var gridImgView1: UIImageView?
    var gridLabel1: UILabel?
    
    var gridBtn2 : UIButton?
    var gridImgView2: UIImageView?
    var gridLabel2: UILabel?
    
    var gridBtn3 : UIButton?
    var gridImgView3: UIImageView?
    var gridLabel3: UILabel?
    
    var gridBtn4 : UIButton?
    var gridImgView4: UIImageView?
    var gridLabel4: UILabel?
    
    var lineView :UIView?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.white
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        self.iconImageView = UIImageView.init(image: UIImage.init(named: "icon_home_helper_top"))
        self.contentView.addSubview(self.iconImageView!)
        iconImageView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview()
            make.width.equalTo(88.5)
            make.height.equalTo(31)
            make.top.equalToSuperview()
        }
        
        self.gridBtn1 = UIButton.init(type: .custom)
        self.gridBtn1?.tag = 1001
        self.gridBtn1?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.contentView.addSubview(self.gridBtn1!)
        self.gridBtn1?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth)/4)
            make.left.equalToSuperview()
            make.height.equalTo(65)
            make.top.equalTo((iconImageView?.snp.bottom)!).offset(20)
        }
        
        self.gridImgView1 = UIImageView.init()
        self.gridBtn1?.addSubview(self.gridImgView1!)
        self.gridImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel1 = UILabel.init()
        self.gridLabel1?.font = UIFont.systemFont(ofSize: 14)
        gridLabel1?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel1?.textAlignment = .center
        self.gridBtn1?.addSubview(self.gridLabel1!)
        self.gridLabel1?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView1?.snp.bottom)!).offset(6)
        }
        
        self.gridBtn2 = UIButton.init(type: .custom)
        self.gridBtn2?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn2?.tag = 1002
        self.gridBtn2?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.contentView.addSubview(self.gridBtn2!)
        self.gridBtn2?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn1?.snp.right)!)
            make.top.equalTo((iconImageView?.snp.bottom)!).offset(20)
        }
        
        self.gridImgView2 = UIImageView.init()
        self.gridBtn2?.addSubview(self.gridImgView2!)
        self.gridImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel2 = UILabel.init()
        self.gridLabel2?.font = UIFont.systemFont(ofSize: 14)
        gridLabel2?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel2?.textAlignment = .center
        self.gridBtn2?.addSubview(self.gridLabel2!)
        self.gridLabel2?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView2?.snp.bottom)!).offset(6)
        }
        
        self.gridBtn3 = UIButton.init(type: .custom)
        self.gridBtn3?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn3?.tag = 1003
        self.gridBtn3?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.contentView.addSubview(self.gridBtn3!)
        self.gridBtn3?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn2?.snp.right)!)
            make.top.equalTo((iconImageView?.snp.bottom)!).offset(20)
        }
        self.gridImgView3 = UIImageView.init()
        self.gridBtn3?.addSubview(self.gridImgView3!)
        self.gridImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel3 = UILabel.init()
        self.gridLabel3?.font = UIFont.systemFont(ofSize: 14)
        gridLabel3?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel3?.textAlignment = .center
        self.gridBtn3?.addSubview(self.gridLabel3!)
        self.gridLabel3?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView3?.snp.bottom)!).offset(6)
        }
        
        self.gridBtn4 = UIButton.init(type: .custom)
        self.gridBtn4?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn4?.tag = 1004
        self.gridBtn4?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.contentView.addSubview(self.gridBtn4!)
        self.gridBtn4?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn3?.snp.right)!)
            make.top.equalTo((iconImageView?.snp.bottom)!).offset(20)
        }
        self.gridImgView4 = UIImageView.init()
        self.gridBtn4?.addSubview(self.gridImgView4!)
        self.gridImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel4 = UILabel.init()
        self.gridLabel4?.font = UIFont.systemFont(ofSize: 14)
        gridLabel4?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel4?.textAlignment = .center
        self.gridBtn4?.addSubview(self.gridLabel4!)
        self.gridLabel4?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView4?.snp.bottom)!).offset(6)
        }
        
        self.lineView = UIView.init()
        self.lineView?.backgroundColor = HexRGBAlpha(0xf6f5fa, 1)
        self.contentView.addSubview(self.lineView!)
        self.lineView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(10)
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    func setData(dict1:[String:Any]?,dict2:[String:Any]?,dict3:[String:Any]?,dict4:[String:Any]?,row:Int) -> Void {
        //        self.gridBtn1?.isHidden = true
        //        self.gridBtn2?.isHidden = true
        //        self.gridBtn3?.isHidden = true
        //        self.gridBtn4?.isHidden = true
        self.gridBtn1?.tag = 1001
        self.gridBtn2?.tag = 1002
        self.gridBtn3?.tag = 1003
        self.gridBtn4?.tag = 1004
        if dict1 != nil {
            self.gridBtn1?.isHidden = false
            self.gridImgView1?.sd_setImage(with:URL(string:(dict1?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel1?.text = (dict1?["title"] as? String) ?? ""
        }
        
        if dict2 != nil {
            self.gridBtn2?.isHidden = false
            self.gridImgView2?.sd_setImage(with:URL(string:(dict2?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel2?.text = (dict2?["title"] as? String) ?? ""
        }
        
        if dict3 != nil {
            self.gridBtn3?.isHidden = false
            self.gridImgView3?.sd_setImage(with:URL(string:(dict3?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel3?.text = (dict3?["title"] as? String) ?? ""
        }
        
        if dict4 != nil {
            self.gridBtn4?.isHidden = false
            self.gridImgView4?.sd_setImage(with:URL(string:(dict4?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel4?.text = (dict4?["title"] as? String) ?? ""
        }
    }
    
    @objc func actionToClick(_ sender:UIButton) -> Void {
        if helperBtnClickBlock2 != nil{
            helperBtnClickBlock2!(sender.tag)
        }
    }
}
